(function (doc, win) {
    //页面如果单独设置fontSzie=0;则不使用
    try{fontSzie}catch(err){fontSzie = 1;}
    if(fontSzie){
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function () {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                docEl.style.fontSize = 100 * (clientWidth / 1080) + 'px';
            };

        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    }
})(document, window);

var AddressHost = {
    "http://dev.xdq-m.haodai.net/":"http://dev.xdyapi.haodai.net",
    "http://test.xdq-m.haodai.net/":"http://test.xdyapi.haodai.net",
    "http://release.xdq-m.haodai.net/":"http://release.xdyapi.haodai.net",
    "http://xdqm.haodai.com/":"http://xdyapi.haodai.com"
};
var localUrl = window.location.href.match(/^https?:\/\/.+?\//);
var host = {};
if(window.location.host.indexOf('localhost') >= 0){
    host.href = 'http://dev.xdyapi.haodai.net';
}else{
    host.href = AddressHost[localUrl];
}

var global = {
    //加载动画...
    loading:function (on) {
        if(on == false){
            setTimeout(function () {
                $('body').find('.loading_cover').hide();
                localStorage.setItem('loading',false);
            },300);
        }else{
            var strVar = "";
            strVar += "<div class=\"loading_cover\"><div><div class=\"content\"><div class=\"rloader\">loading<\/div><p>加载中...<\/p><\/div><\/div><\/div>";
            $('body').append(strVar);
            localStorage.setItem('loading',true);
        }
    }
};

//判断是否是存在
var isNull = function(data){
    return (data == "" || data == undefined || data == null || data == false) ? false : true;
};

//数组操作
var arrData = {
    //序列化表单转数组
    serializeArray:function (mailAddress) {
        var tempNameArr = {};
        $.each(mailAddress, function(index, val) {
            tempNameArr[val['name']] = val['value'];
        });
        return tempNameArr;
    }
};

//get操作
var get  = {
    /**
     * 获取加密的id参数
     * @param name 名称
     * @param split 分隔符
     * @returns {*}
     */
    id:function (name,split) {
        name = name == undefined ? 'id' : name;
        split = split == undefined ? '/' : split;
        var id = get.init(name);
        if(id == '') return false;
        if(split){
            parameter = get.de(id).split(split); //挂全局
        }else{
            parameter = get.de(id); //挂全局
        }
        return parameter;
    },
    /**
     * 解密字符串
     * @param str 传入字符串
     * @returns {string}
     */
    de:function (str) {
        return window.atob(str);
    },
    /**
     *  获取地址栏的参数
     * @param name 参数名
     * @returns {*}
     */
    init:function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return '';
    }
};

//post操作
var set = {
    id:function (str) {
        return set.init(str);
    },
    /**
     * 加密字符串
     * @param str 传入需要加密的参数
     * @returns {*}
     */
    en:function (str) {
        return window.btoa(str);
    },
    init:function (str) {
        return window.btoa(str);
    }
};

// 异步接口封装
var ajax = {
    post:function (url,obj,status,upjax,errprompt){
        var resObj;
        errprompt = errprompt == undefined?'连接服务器失败...':errprompt;
        $.ajaxSetup({async:false});
        urls = ajax.comm(url);
        if(upjax) urls = url;
        try{
            $(document).ajaxError(function (o, xhr, property, err) {
                throw new Error(err); //抛出异常
            });
            $.post(urls,obj,function(data, textStatus, xhr) {
                resObj = {data:data,status:textStatus,error:xhr};
                if(!status) resObj = data;
            });
            return resObj;
        }catch(err){
            console.info(err);
            layer.msg('Orz...'+errprompt);
            return false;
        }
    },
    get:function (url,status,upjax,errprompt){
        var resObj;
        errprompt = errprompt == undefined?'连接服务器失败':errprompt;
        $.ajaxSetup({async:false});
        if(!upjax) url = ajax.comm(url);
        try{
            $(document).ajaxError(function (o, xhr, property, err) {
                throw new Error(err);//抛出异常
            });
            $.get(url, function(data, textStatus, xhr) {
                resObj = {data:data,status:textStatus,error:xhr};
                if(!status) resObj = data;
            });
            return resObj;
        }catch(err){
            console.info(err);
            layer.msg('Orz...'+errprompt);
            return false;
        }
    },
    comm:function (url) {
        var app_version = isNull(app_version) == false?34000:app_version;
        var xrz = '?';
        if(url.indexOf('?') > 0) xrz = '&';
        return host.href+'/' + url + xrz + 'app_version=' + app_version;
    }
};

// 浏览器终端UA判断
var browser = {
    versions: function () {
        var u = navigator.userAgent, app = navigator.appVersion;
        return {
            mobile: !!u.match(/AppleWebKit.*Mobile.*/),      //是否为移动终端
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
            weixin: u.indexOf('MicroMessenger') > -1,
        };
    }(),
    language: (navigator.browserLanguage || navigator.language).toLowerCase()
};

// APP方法跳转
var app = {
    /**传入跳转地址配置
     * @auth  天明
     * @param conf
     */
    location:function (conf) {
       try{
           var config = {
               ios: 'haodai://www.xindaiquan.com'+conf['ios'],
               android: 'haodai://www.xindaiquan.com'+conf['android'],
               weixin:conf['weixin'],
               mobile:conf['mobile'],
               default:conf['default']
           };
           if (browser.versions.ios == true && config.ios != undefined) {
               location.href = config.ios;
           } else if(browser.versions.android == true && config.android != undefined){
               location.href = config.android;
           }else if(browser.versions.weixin ==true && config.weixin != undefined){
               location.href = config.weixin;
           }else if(browser.versions.mobile == true && config.mobile != undefined){
               location.href = config.mobile;
           }else{
               console.log(config);
               console.log(config.ios);
               location.href = config.ios;
           }
       }catch (err){
           layer.msg(JSON.stringify(err));
           console.log(err);
       }
    }
};

//纺织重复提交的方法
var submitlock = {
    time:function () {
        return new Date().getTime();
    },
    btn:function (is,promot) {
        $(is).text(promot);
    },
    lock : function (is,wait,prompt) {
        wait = wait == undefined ? 1000 : wait;
        prompt = prompt == undefined ? '操作频繁,请稍后...': prompt;
        if($(is).attr('date') == undefined){
            $(is).attr('date',this.time());
            return true;
        }else{
            if(this.time() - $(is).attr('date') <= wait){
                layer.msg(prompt);
                return false;
            }else{
                $(is).attr('date',this.time());
                return true;
            }
        }
    },
    /**
     *  提交中...
     * @param status 状态 true, 开始, false, 关闭
     * @param confim 提示文字
     */
    loading:function (status,confim) {
        confim = confim == undefined ? '加载中...':confim;
        status = status == undefined ? true : status;
        function addendHtml(confim) {
            var str = '<div class="loading_cover_opacity" style="display:block"><div><div class="content"><div class="opacity-cover"><div class="la-line-spin-clockwise-fade"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div><p>'+confim+'</p></div></div></div></div>';
            $('#mainview').append(str);
        }
        if(status == true){
            addendHtml(confim);
        }else if (status == false) {
            $('.loading_cover_opacity').hide();
        }else{
            addendHtml(confim);
        }
    }
};
